package com.qdzsaas.dbwd.live.manager

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import com.qdzsaas.dbwd.live.model.LiveMessageModel
import com.qdzsaas.dbwd.live.model.LiveMessageModel.*
import com.talkfun.cloudlivepublish.consts.MemberRole.MEMBER_ROLE_USER


//直播间 - 左下角公屏RV - 其中一条Item消息 - TextView的SpannableString的昵称String
class LiveMessageTextSpanNameStrategy: LiveMessageTextSpanStrategy {

    //用户昵称
    private val nameColorSpan = ForegroundColorSpan(Color.parseColor("#8CE7FF"))
    //主播昵称
    private val anchorColorSpan = ForegroundColorSpan(Color.parseColor("#F628BA"))

    //否需要显示昵称String
    override fun spanAppend(model: LiveMessageModel, currentIndex: Int, totalSpannableString: SpannableStringBuilder): Int {
        model.chatBean.nickname?.let {
            totalSpannableString.append(it)
            var nameTotalLength = it.length
            if (model.type == TYPE_MESSAGE){
                //是用户写的文字消息
                totalSpannableString.append("：")
                nameTotalLength += 1 //+1是那个冒号
            } else if (model.type == TYPE_COMING){
                //是用户进入房间
                totalSpannableString.append(" ")
                nameTotalLength += 1 //+1是那个空格

            } else if (model.type == TYPE_GIFT){
                //是礼物
                totalSpannableString.append(" ")
                nameTotalLength += 1 //+1是那个空格

            }
            if(model.chatBean.role.equals(MEMBER_ROLE_USER))
            totalSpannableString.setSpan(nameColorSpan, currentIndex, currentIndex + nameTotalLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
            else
                totalSpannableString.setSpan(anchorColorSpan, currentIndex, currentIndex + nameTotalLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
            return nameTotalLength
        }
        return 0
    }
}